﻿var validator = {
    Require: /.+/,
    Email: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
    Phone: /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,8}(\-\d{1,5})?$/,
    Mobile: /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/,
    Url: /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
    Number: /^\d+$/,
    Zip: /^[1-9]\d{5}$/,
    QQ: /^[1-9]\d{4,8}$/,
    Integer: /^[-\+]?\d+$/,
    Double: /^[-\+]?\d+(\.\d+)?$/,
    UnSafe: /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
    IsSafe: function(str) { return !this.UnSafe.test(str); }
};

var djblog = djblog || {};
djblog.url = {
    ajaxpost: "/ashx/post.ashx"
}
djblog.comment = {
    intervalid: null, //
    req: ["author", "email", "comment", "url", "postid"], //提交变量
    //验证变量
    valite: function() {
        if (!$.trim($("#" + djblog.comment.req[0]).val())) {
            var _el = $("#" + djblog.comment.req[0]);
            _el.num = 4;
            intervalid = setInterval(djblog.comment.border(_el), 200);
            return false;
        }
        if (!$.trim($("#" + djblog.comment.req[1]).val())) {
            var _el = $("#" + djblog.comment.req[1]);
            _el.num = 4;
            intervalid = setInterval(djblog.comment.border(_el), 200);
            return false;
        }
        if (!$.trim($("#" + djblog.comment.req[2]).val())) {
            var _el = $("#" + djblog.comment.req[2]);
            _el.num = 4;
            intervalid = setInterval(djblog.comment.border(_el), 200);
            return false;
        }
        return true;
    },
    //提交评论
    com: function() {
        $("#submit").bind("click", function() {
            if (djblog.comment.valite()) {
                $.ajax({
                    type: "POST",
                    url: djblog.url.ajaxpost + "?act=addcomment",
                    dataType: "json",
                    data: { author: $("#" + djblog.comment.req[0]).val(), email: $("#" + djblog.comment.req[1]).val(), comment: $("#" + djblog.comment.req[2]).val(), url: $("#" + djblog.comment.req[3]).val(), postid: $("#" + djblog.comment.req[4]).val() },
                    success: function(d) {
                        switch (d.ret) {
                            case 1:
                                $("#comments").append(djblog.comment.addcommenthtml(d));
                                djblog.comment.clear();
                                break;
                            case 2:
                                alert("系统忙!");
                                break;
                        }
                    },
                    error: function() {

                    }
                });
            }
        });
    },
    //边框变色
    border: function(_el) {
        return function() {
            if (_el.num > 0) {
                if (_el.num & 1) {
                    _el.css("borderColor", "#ddd");
                } else {
                    _el.css("borderColor", "#ff0000");
                }
                _el.num--;
            } else {
                _el.focus();
                clearInterval(intervalid);
            }
        }
    },
    addcommenthtml: function(d) {
        var comhtml;
        comhtml = '<div class="bbslist">' +
            '<div class="comment">' +
                '<div style="margin: 0px; border: currentColor;" class="bbslist">' +
                    '<dl style="padding: 5px 0px; border: currentColor;" class="dlClass">' +
                        '<dt><img alt="" src="../image/nohead.png" width="54" height="54" /></dt>' +
                        '<dd class="bSt">' +
                            '<b><a href="#" rel="external nofollow">' + d.author + '</a></b> <span>' + d.time + '</span>' +
                        '</dd>' +
            '<dd>' +
            '<p>' + d.content + '</p>' +
                            '<span><a class="comment-reply-link" href="#">Reply</a>' +
                                '<a title="Permanent Link for this comment" href="#comment-90"></a>' +
                            '</span>' +
                        '</dd>' +
                    '</dl>' +
                '</div>' +
            '</div>' +
            '</div>';
        return comhtml;
    },
    clear: function() {
        for (var i = 0; i < 4; i++) {
            $("#" + djblog.comment.req[i]).val("");
        }
    }
};

djblog.search = {
    searchbtn: $("#searchsubmit"),
    searchval: $("#s"),
    init: function() {
        djblog.search.searchbtn.click(function() {
            var searchval = encodeURIComponent(djblog.search.searchval.val());
            location.href = searchval ? "/blog/" + searchval + ".html" : "/blog/";
        });
    }
};

djblog.sliber = {
    sliul: $("#slider ul"),
    pre: $("#prevBtn"),
    next: $("#nextBtn"),
    marginVal: 2,
    evenval: 988,
    init: function() {
        var that = djblog.sliber;
        that.pre && that.pre.click(that.prefn);
        that.next && that.next.click(that.nextfn);
    },
    prefn: function() {
        var that = djblog.sliber;
        if (that.marginVal >= 2) {
            return;
        }
        that.marginVal++;
        that.sliul && that.sliul.animate({ marginLeft: "-" + that.marginVal * that.evenval + "px" }, 2500);
        return false;
    },
    nextfn: function() {
        var that = djblog.sliber;
        if (that.marginVal <= 0) {
            return;
        }
        that.marginVal--;
        that.sliul && that.sliul.animate({ marginLeft: "-" + that.marginVal * that.evenval + "px" }, 2500);
        return false;
    }
};

$(function() {
    djblog.comment.com();
//    djblog.sliber.init();
    djblog.search.init();
    $("img").lazyload({
        placeholder: "http://liumiao.me/lazyload/grey.gif",
        effect: "fadeIn"
    });

});




